#include "stdafx.h"
#include "Property.h"

#include "../db/ItemProperty.h"
#include "PropertyTemplate.h"
#include "PropertyDescription.h"

#ifdef WIN32
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif //_DEBUG
#endif //WIND32

namespace serika
{
	namespace dq
	{
		Property::Property(db::ItemProperty* dbProperty, TransientState::Enum state)
		{
			mProperty = new ItemProperty(dbProperty, state);
		}

		Property::~Property()
		{
			delete mProperty;
		}

		// static
		Property* Property::NewProperty(int itemId, int reserved1, int reserved2, int reserved3, const ut::String& reserved4,
			PropertyTemplate* propertyTemplate)
		{
			db::ItemProperty* dbProperty = new db::ItemProperty;

			int templateId = propertyTemplate->GetId();

			dbProperty->SetValue("item_id", itemId);
			dbProperty->SetValue("template_id", templateId);
			dbProperty->SetValue("reserved1", reserved1);
			dbProperty->SetValue("reserved2", reserved2);
			dbProperty->SetValue("reserved3", reserved3);
			dbProperty->SetValue("reserved4", reserved4);
			ut::String description;
			const ut::String& descriptionTemplate = propertyTemplate->GetDescription();
			PropertyDescription::GetDescription(templateId, descriptionTemplate,
				reserved1, reserved2, reserved3, reserved4, description);
			dbProperty->SetValue("description", description);

			return new Property(dbProperty, TransientState::New);
		}

		// static
		Property* Property::FromDb(db::ItemProperty* dbProperty)
		{
			return new Property(dbProperty, TransientState::Clean);
		}

		int Property::GetId() const
		{
			return mProperty->GetInt("id");
		}

		int Property::GetTemplateId() const
		{
			return mProperty->GetInt("template_id");
		}

		int Property::GetItemId() const
		{
			return mProperty->GetInt("item_id");
		}

		int Property::GetReserved1() const
		{
			return mProperty->GetInt("reserved1");
		}

		int Property::GetReserved2() const
		{
			return mProperty->GetInt("reserved2");
		}

		int Property::GetReserved3() const
		{
			return mProperty->GetInt("reserved3");
		}

		const ut::String& Property::GetReserved4() const
		{
			return mProperty->GetString("reserved4");
		}

		const ut::String& Property::GetDescription() const
		{
			return mProperty->GetString("description");
		}

		void Property::SetItemId(int itemId)
		{
			mProperty->Set("item_id", itemId);
		}

		void Property::SetReserved1(int reserved1)
		{
			mProperty->Set("reserved1", reserved1);
		}

		void Property::SetReserved2(int reserved2)
		{
			mProperty->Set("reserved2", reserved2);
		}

		void Property::SetReserved3(int reserved3)
		{
			mProperty->Set("reserved3", reserved3);
		}

		void Property::SetReserved4(const ut::String& reserved4)
		{
			mProperty->Set("reserved4", reserved4);
		}

		void Property::Save()
		{
			mProperty->Save();
		}

		void Property::Delete()
		{
			mProperty->Delete();
		}
	}
}
